/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
    //1，将所用的变量都抽取出来，放在页面加载后
    var att_datagrid = $("#att_datagrid");
    var att_dialog = $("#att_dialog");
    var att_form = $("#att_form");
    var import_dialog = $("#import_dialog");
    att_datagrid.datagrid({
        url: '/attendance/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: '#tb',
        onClickRow: function (index, row) {
            if (row.state) {
                $("#changeState_btn").linkbutton({
                    text: '离职'
                })
            } else {
                $("#changeState_btn").linkbutton({
                    text: '复职'
                })
            }
        },
        columns: [[
            {field: 'date', title: '日期', width: 100, sortable: true},
            {field: 'employee', title: '员工姓名', width: 100,formatter:function (value) {
                return value? value.username:"";
            }},
            {field: 'dept', title: '部门', width: 100,formatter:function (value) {
                return value? value.name:"";
            }},
            {field: 'timeIn', title: '上班时间', width: 100, sortable: true},
            {field: 'timeOot', title: '下班时间', width: 100, sortable: true},
            {
                field: 'state', title: '考勤状态', width: 100, formatter: function (value) {
                return value ? "<font color='green'>正常</font>" : "<font color='red'>异常</font>";
            }
            }
        ]]
    })
    att_dialog.dialog({
        width: 320,
        height: 350,
        buttons: '#btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            att_form.form('clear');
        }
    })
    import_dialog.dialog({
        width: 350,
        height: 100,
        closed: true
    })

    var methodObj = {
        add: function () {
            //设置标题
            att_dialog.dialog('setTitle', '新增员工');
            //打开弹出框
            $("#password_tr").show();
            att_dialog.dialog('open');
        },
        edit: function () {
            var row = att_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }
            $("#password_tr").hide();
            if (row.dept) {
                row["dept.id"] = row.dept.id;
            }
            $.get('/attendance/getRoleIdByEmpId.do', {attendanceId: row.id}, function (data) {
                $("#role_combobox").combobox('setValues', data)
            })
            //把数据回显到表单中
            att_form.form('load', row);
            //设置标题
            att_dialog.dialog('setTitle', '编辑员工');
            //打开弹出框
            att_dialog.dialog('open');

        },
        changeState: function () {
            var row = att_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if (r) {
                    $.get('/attendance/changeState.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                att_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', 'data.msg', 'warning');
                        }
                    })
                }
            });
        },
        changePwd: function () {
            var row = att_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if (r) {
                    $.get('/attendance/changePassword.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                att_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', 'data.msg', 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            att_datagrid.datagrid('reload');
        },
        save: function () {
            //提交ajax
            att_form.form('submit', {
                url: '/attendance/saveOrUpdate.do',
                onSubmit: function (param) {
                    var ids = $("#role_combobox").combobox('getValues');
                    for (var i = 0; i < ids.length; i++) {
                        //提交额外的参数
                        param['roles[' + i + '].id'] = ids[i];
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            //在dataGrid重新加载数据
                            att_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', 'data.msg', 'warning');
                    }
                }
            })
        },
        cancel: function () {
            att_dialog.dialog('close');
        },
        query: function () {
            var keyword = $("[name=keyword]").val();
            var deptId = $("[name=deptId]").val();
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            console.log(keyword);
            console.log(deptId);
            att_datagrid.datagrid('load', {
                keyword: keyword,
                deptId: deptId,
                beginTime: beginTime,
                endTime: endTime
            })
        },
        exportXls: function () {
            var keyword = $("[name=keyword]").val();
            var deptId = $("[name=deptId]").val();
            window.location.href = "/attendance/exportXls.do?keyword=" + keyword + "&deptId=" + deptId;
        },
        importXls: function () {
            import_dialog.dialog('open');
        }
    }
    //3，给按钮统一绑定点击事件
    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
function fileSubmit() {
    var fileSize = $("#file").val();
    if(fileSize!=null && fileSize != ''){
        console.log("bu");
        $("#import_form").submit();
    }else {
        $.messager.alert('温馨提示', '请选中要上传的文件！', 'warning');
    }
}
